package com.lyhengtongwl.zqsnews.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PosterAdapter extends PagerAdapter {

    private List<String> viewList;
    private LayoutInflater mInflater;
    public PosterAdapter(Context context, List<String> viewList) {
        this.viewList = viewList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = mInflater.inflate(R.layout.item_page, null);
        LinearLayout root = (LinearLayout) view.findViewById(R.id.root);
        RoundedImageView mTv = (RoundedImageView) view.findViewById(R.id.banner_image);
        Picasso.with(App.getContext()).load(viewList.get(position)).into(mTv);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }
}
